<template>
  <el-drawer v-model="drawer" :direction="direction" @close="closeclick">
    <template #title>
      <h4>{{ title }}</h4>
    </template>
    <template #default>
      <el-form
        ref="ruleFormRef"
        :model="tableData"
        status-icon
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
      >
        <el-form-item label="工位名称" prop="stationName">
          <el-input
            v-model="tableData.stationName"
            clearable
            placeholder="请输工位间名称"
          />
        </el-form-item>
        <el-form-item label="工位位置" prop="stationPosition">
          <el-input
            v-model="tableData.stationPosition"
            clearable
            placeholder="请输入隔间编号"
          />
        </el-form-item>
        <el-form-item label="拍照次数" prop="stationPhotoCount">
          <el-input
            v-model="tableData.stationPhotoCount"
            clearable
            placeholder="请输入拍照次数"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            @click="submitForm(ruleFormRef, tableData)"
            >提交</el-button
          >
          <el-button @click="resetForm(ruleFormRef)">取消</el-button>
        </el-form-item>
      </el-form>
    </template>
    <template #footer> </template>
  </el-drawer>
</template>

<script  setup>
import { ref, defineProps, watch, defineEmits, reactive } from 'vue'
import { stationUpdate } from '@/api/station'
import { ElMessage } from 'element-plus'

const props = defineProps({
  title: {
    type: String
  },
  showDrawer: {
    type: Boolean,
    // 必传
    required: true
  },
  amendStation: {
    type: Object
  }
})
// 定义一个变量 接收父组件传递来的值
const tableData = reactive({})
// 侦听传递过来的数据
watch(
  () => props.amendStation,
  (value) => {
    // console.log(value)
    Object.assign(tableData, value)
    // tableData = value
    // console.log(tableData)
  },
  {
    immediate: true,
    deep: true
  }
)

// 表单校验
const ruleFormRef = ref()

const rules = reactive({
  stationName: [
    {
      required: true,
      message: '工位名称不能为空',
      trigger: 'blur'
    }
  ],
  stationPosition: [
    {
      required: true,
      message: '工位位置不能为空',
      trigger: 'blur'
    }
  ],
  stationPhotoCount: [
    {
      required: true,
      message: '工位位置不能为空',
      trigger: 'blur'
    }
  ]
})
// 提交
const loading = ref(false)
const submitForm = (formEl, tableData) => {
  console.log(tableData)
  const { stationId, stationName, stationPosition } = tableData
  formEl.validate((valid) => {
    if (valid) {
      loading.value = true
      setTimeout(() => {
        drawer.value = false
        loading.value = false
        // 验证通过调用修改接口
        stationUpdate({
          stationId: stationId,
          stationName: stationName,
          stationPosition: stationPosition
        })
          .then((res) => {
            ElMessage({
              message: res.message,
              type: 'success'
            })
            emit('clickShow', false, res.code)
          })
          .catch((err) => {
            ElMessage.error(err.message)
            emit('clickShowChange', false)
          })
      }, 500)
    } else {
      ElMessage.error('失败')
      return false
    }
  })
}
// 取消提交
const resetForm = (formEl) => {
  if (!formEl) return
  formEl.resetFields()
  drawer.value = false
  emit('clickShowChange', false)
}

// 抽屉的隐藏显示
const emit = defineEmits(['clickShowChange'])
const drawer = ref(false)
watch(
  () => props.showDrawer,
  (value) => {
    drawer.value = value
    // console.log(drawer.value)
  }
)

// 抽屉方向  rtl / ltr / ttb / btt
const direction = ref('rtl')
// 监听抽屉关闭
const closeclick = () => {
  emit('clickShowChange', false)
  ruleFormRef.value.resetFields()
}
</script>
